package mwa.cs.rq.ctrl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * Servlet implementation class CS66000Srvl
 */
public class CS66000Srvl extends HttpServlet {
	private static final long serialVersionUID = 1L; 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CS66000Srvl() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String vaProcessType = "";
    	vaProcessType =   request.getParameter("process_type");
//		System.out.println("Process_type : "+process_type);
		if(vaProcessType=="getRequestType"||vaProcessType.equals("getRequestType")){
			getRequestType(request,response);
		}
		else if(vaProcessType=="getRequestStatus"||vaProcessType.equals("getRequestStatus")){
			getRequestStatus(request,response);
		}
		
	}
	private void getRequestStatus(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String[] vaValue = new String[4];
		String[] vaMessage = new String[4];
		
		vaValue[0] = "1";
		vaValue[1] = "2";
		vaValue[2] = "3";
		vaValue[3] = "4";
		
		
		vaMessage[0] = "รับคำร้อง";
		vaMessage[1] = "รับเงิน";
		vaMessage[2] = "ปิดคำร้อง";
		vaMessage[3] = "ยกเลิก";
		
		JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter voOut = response.getWriter();
		if (vaMessage != null) {
			for (int i = 0; i < vaMessage.length; i ++) {
				voList.add(vaMessage[i]);
			}
		}
		voObject.put("messages", voList);
		JSONArray voList2 = new JSONArray();
		if (vaValue != null) {
			for (int i = 0; i < vaValue.length; i ++) {
				voList2.add(vaValue[i]);
			}
		}
		voObject.put("values", voList2);
		
		voOut.print(voObject.toJSONString());
		
	}

	public void getRequestType(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		
		String[] vaValue = new String[3];
		String[] vaMessage = new String[3];
		
		vaValue[0] = "01";
		vaValue[1] = "02";
		vaValue[2] = "03";
		
		vaMessage[0] = "คำร้องขอติดตั้งประปา";
		vaMessage[1] = "คำร้องเกี่ยวกับมาตร";
		vaMessage[2] = "คำร้องอื่นๆ";
		
		JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter voOut = response.getWriter();
		if (vaMessage != null) {
			for (int i = 0; i < vaMessage.length; i ++) {
				voList.add(vaMessage[i]);
			}
		}
		voObject.put("messages", voList);
		JSONArray voList2 = new JSONArray();
		if (vaValue != null) {
			for (int i = 0; i < vaValue.length; i ++) {
				voList2.add(vaValue[i]);
			}
		}
		voObject.put("values", voList2);
		
		voOut.print(voObject.toJSONString());
	}
	

}
